package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.ActivityInputOldSecretPwdBinding;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class InputOldSecretPwdActivity extends BaseActivity {

    private ActivityInputOldSecretPwdBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_input_old_secret_pwd);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        binding.tvModuleOk.setOnClickListener(this);
        binding.ivModuleDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleDelete:
                finish();
                break;
            case R.id.tvModuleOk:
                String pwd = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)
                        || pwd.length() != 6
                ) {
                    ToastUtil.showToast("请输入身份证后六位");
                    return;
                }

                checkIDCard(pwd);
                break;
        }
    }

    private void checkIDCard(String bodyNum) {
        HTTPAPI.getInstance().checkIDCard(bodyNum, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputOldSecretPwdActivity.this, s);
                } else {
                    Intent intent = new Intent(
                            InputOldSecretPwdActivity.this,
                            InputNewSecretPwdActivity.class
                    );
                    intent.putExtra("module_num", bodyNum);
                    startActivity(intent);
                    ActivityStack.getScreenManager().popActivities(InputOldSecretPwdActivity.class);
                }
            }
        });
    }

}
